﻿using CashFlow.Db.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;
using AutoFacUtils;
using CashFlow.Web.Service;
using CashFlow.Web.Models.Fee.Result;
using CashFlow.Web.Common;
using CashFlow.Web.Filters;
using CashFlow.Web.Validator;
using CashFlow.Web.Models;

namespace CashFlow.Web.Controllers
{
    public class FeeController : Controller
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(FeeController));
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IFeeService GetFeeService() { return AutoFacHelper.Get<IFeeService>(); }

        public ActionResult Index()
        {
            var date = MyCommonTool.GetOrderDate();
            var userService = GetUserService();
            var ui = userService.GetLoginUser();
            var feeService = GetFeeService();
            var entity = feeService.QueryByUserId(ui.Id);
            var entity2 = entity ?? new FeeStatisticsInfo();
            var entity3 = feeService.QueryByUserIdDate(ui.Id, date);
            var entity4 = entity3 ?? new FeeEntity();
            ViewBag.StatisticsInfo = entity2;
            ViewBag.Lastest = entity4;

            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(DateModelValidator))]
        public ActionResult QueryByDate(DateModel model)
        {
            var userService = GetUserService();
            var ui = userService.GetLoginUser();
            var feeService = GetFeeService();
            var entity = feeService.QueryByUserIdDate(ui.Id, model.Date);
            var entity2 = entity ?? new FeeEntity();
            return MyCommonTool.CreateOperationResult(true, "", entity2);
        }
    }
}